import * as React from 'react';

import {IView} from '../types/IView';
import {IParentView} from '../types/IParentView';
import {IAttr} from '../types/IAttr';

import {IViewObject} from './IViewObject';
import AbsParentViewObject from './AbsParentViewObject';

import sliderItemViewObject from './SliderItemViewObject';
import Slider from '../components/Slider';

export class SliderPaneViewObject extends AbsParentViewObject<IParentView> implements IViewObject<IParentView> {

  viewType = 'sliderPane';

  attrs: IAttr[] = [
    {
      name: 'id',
      display: 'ID',
      type: 'string',
      required: true,
      defaultValGenerator: () => {
        return `sliderPaneView${this.viewIndex}`;
      }
    },
    {
      name: 'className',
      display: 'class',
      type: 'string',
      required: false,
      defaultValGenerator: () => {
        return '';
      }
    },
    {
      name: 'config',
      display: 'Slide 配置',
      type: 'string',
      required: false,
      defaultValGenerator: () => {
        return '';
      }
    }
  ];

  create(designId: string): IParentView {
    return {
      id: this.generateId(),
      viewType: this.viewType,
      designId,
      style: {},
      attrVals: this.createAttrVals(this.attrs),
      children: [
        // 默认放一个Tab 上去, 不然空的内容, 不然不方便直接添加子组件
        sliderItemViewObject.create(designId)
      ]
    };
  }

  render(view: IParentView, refUpdater: (view: IView, ref: Element) => void): JSX.Element | null {
    const {attrVals} = view;

    const elemRefUpdater: React.Ref<Element> = el => refUpdater(view, el);
    const props: React.HTMLAttributes<HTMLElement> = {
      id: attrVals.id,
      className: attrVals.className,
      style: attrVals.className ? {} : view.style
    };

    return (
      <div ref={elemRefUpdater} {...props}>
        <Slider designId={view.designId} view={view}>
          {view.children.map(child => this.renderChild(child, refUpdater))}
        </Slider>
      </div>
    );
  }

  /**
   * 只能添加sliderItem 组件
   * @param childViewType 要判断的子组件的类型
   * @return 如果可以接收这个子组件, 则返回true
   */
  accept(childViewType: string): boolean {
    return childViewType === sliderItemViewObject.viewType;
  }
}

const sliderPaneViewObject = new SliderPaneViewObject();
export default sliderPaneViewObject;


